package com.fancy.projecty;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.*;
import java.util.List;

/**
 * 历史记录的查询
 * * 需求：
 * *   历史数据的查看
 * *  查询已经跑过的流程
 * *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class G_历史记录的查询 {


	@Autowired
	/*执行管理、包括启动、推进、删除流程实例等操作*/
	private RuntimeService runtimeService;
	@Autowired
	/*任务管理,可以做任务的查询的*/
	private TaskService taskService;
	/*历史记录的查询*/
	@Autowired
	private HistoryService historyService;

	@Autowired
	/*管理流程定义,部署相关的Service*/
	private RepositoryService repositoryService;


	@Test
	public void c_() {
		//创建历史流程实例，查询对象
		HistoricProcessInstanceQuery qingjia01 = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("qingjia01").finished();
		List<HistoricProcessInstance> list = qingjia01.list();
		if (list == null || list.size() == 0) {
			System.err.println("没有已经完成的历史流程实例");
		} else {
			for (HistoricProcessInstance historicProcessInstance : list) {
				System.err.println(historicProcessInstance.toString());
			}
		}
	}


	@Test
	public void c_历史数据的查询() throws IOException {
		//3.得到HistoricActivitiInstanceQuery对象
		HistoricActivityInstanceQuery historicActivityInstanceQuery = this.historyService
				.createHistoricActivityInstanceQuery();
		historicActivityInstanceQuery.processInstanceId("2505");//设置流程实例的id

		//4.执行查询
		List<HistoricActivityInstance> list = historicActivityInstanceQuery
				.orderByHistoricActivityInstanceStartTime().asc().list();//排序StartTime


		if (list != null && list.size() > 0) {
			//5.遍历查询结果
			for (HistoricActivityInstance instance : list) {
				System.out.println(instance.getActivityId());
				System.out.println(instance.getActivityName());
				System.out.println(instance.getProcessDefinitionId());
				System.out.println(instance.getProcessInstanceId());
				System.out.println("=============================");
			}
		}


	}

}
